//
//  RegisterVM.swift
//  LeTu
//
//  Created by g1 on 2023/12/4.
//

import Foundation

class RegisterVM {
    
    /// 用户注册
    func register(params: [String: Any], completeHandle: @escaping NetworkRequestHandle) {
        
        apiProvider.request(api: .register(params: params)) { json, jsonArr, code in
            
            MYLog("用户注册完成：",json)
            
            var dialog = DialogMessage()
            
            guard code == 200, let pm = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = pm.code
            dialog.message = pm.msg
            // 解析data数据
            guard let data = pm.data as? [String : Any], let responseModel = Mapper<LoginResp>().map(JSON: data), pm.code == 200 && responseModel.token.count > 0  else { return completeHandle(dialog) }
            
            // 保存用户信息
            kUserManager.loginSaveToken(resp: responseModel)
            completeHandle(dialog)
            
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
        
    }
    
}
